// Test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "cfixedframerate.h"
#include "ccube.h"

#define MOVE_STEP .1
#define ANGLE_STEP 0.2
#define PI 3.1416f

float g_x=0.0f;
float g_y=0.0f;
float g_z=10.0f;
float g_roll= 0.0f;
float g_pitch= 0.0f;
float g_yaw= 0.0f;
int g_w;
int g_h;
float g_cubeAngle= 0.f;

int xMouse = 683;

boolean t8 = false;
boolean t6 = false;
boolean t4 = false;
boolean t2 = false;
boolean solid = false;

CCube cube1;
CCube cube2;

void Keyboard(unsigned char key,int x, int y)
{
	//keyboard callback function	
	switch (key){
	case '8':	t8 = true; break;
	case '2':	t2 = true; break;
	case '6':	t6 = true; break;
	case '4':	t4 = true; break;
	case 'c':	if(!glIsEnabled(GL_CULL_FACE)){
					glEnable(GL_CULL_FACE);
					glCullFace(GL_BACK);
				  };
	case 'm':	if(!solid) solid = true;
				else solid = false;
				break;
	case 27: exit(0);
	}
}

void KeyboardUp(unsigned char key,int x, int y)
{
	//keyboard callback function	
	switch (key){
	case '8':	t8 = false; break;
	case '2':	t2 = false; break;
	case '6':	t6 = false; break;
	case '4':	t4 = false; break;
	case 27: exit(0);
	}
}

void raton (int x, int y){
	g_yaw = g_yaw  + (x-xMouse)*0.1;
	xMouse = x;
}
void Set3DView()
{
	//set projection matrix
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);

	//set modelview matrix
	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity ();
	glRotatef(-g_roll, 0.0, 0.0, 1.0);	
	glRotatef(-g_yaw, 0.0, 1.0, 0.0);
	glRotatef(-g_pitch, 1.0, 0.0, 0.0);	
	glTranslatef(-g_x, -g_y, -g_z);
}



void DrawScene(void)
{
	//clean the backbuffer
	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light_difuse);
	glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);

	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
	glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);

	glEnable(GL_LIGHTING);
	glNormal3fv(n1);

	//viewing transformation
	Set3DView();

	//draw the cube1
	cube1.setColor(255, 0, 0);
	cube1.setScale(0.5, 0.5, 0.5);
	cube1.setPosition(0, 0.5, 0);
	//cube1.draw(true);// con true hace que se dibujen las caras y con false las aristas

	//draw the cube2
	cube2.setColor(0, 255, 0);
	cube2.setScale(2, 2, 2);
	cube2.setPosition(0, 0, -3);
	cube2.draw(solid);
	cube1.draw(solid);
}

void Reshape (int w, int h)
{
	//Reshape callback function
	g_h= h;
	g_w= w;
	glViewport (0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
}

int main(int argc, char** argv){
	
	glEnable(GL_DEPTH_TEST);

	//INIT GLUT/////////////////////
	////////////////////////////////
	//init window and OpenGL context
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize (1024, 768); 
	glutCreateWindow (argv[0]);
	glutFullScreen();

	//callback functions
	glutDisplayFunc(DrawScene); 
	glutReshapeFunc(Reshape); // cuando se redimensiona la ventana
	glutKeyboardFunc(Keyboard); //la tecla que ha sido pulsada
	glutKeyboardUpFunc(KeyboardUp); //la tecla que ha sido soltada
	glutPassiveMotionFunc(raton);	

	CFixedFrameRate *pFFR = new CFixedFrameRate(10);
	
	while (1)
	{
		//UPDATE////////////////////
		////////////////////////////
		//"move" the cube
		g_cubeAngle+= 0.1;
		//queued events?
		glutMainLoopEvent();

		if(t8){	
			g_x-= MOVE_STEP*sin(g_yaw*PI/180);
			g_z-= MOVE_STEP*cos(g_yaw*PI/180);
		}
		if(t2){
			g_x+= MOVE_STEP*sin(g_yaw*PI/180);
			g_z+= MOVE_STEP*cos(g_yaw*PI/180);
		}
		if(t6)
			g_yaw-= ANGLE_STEP;
		if(t4)
			g_yaw+= ANGLE_STEP;

		//RENDER////////////////////
		////////////////////////////
		glutPostRedisplay();
		glutSwapBuffers();

		pFFR->endFrame();
	}

	delete pFFR;// hacer el delete siempre que hagamos un new
   return 0;
}